package org.reservation.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.reservation.dao.RestaurantDAO;
import org.reservation.pojo.Manager;
import org.reservation.pojo.Restaurant;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


public class RestaurantDAOHiberImpl extends HibernateDaoSupport implements RestaurantDAO{

	public void addRes(Restaurant restaurant) {
		// TODO Auto-generated method stub
		getHibernateTemplate().save(restaurant);
	}

	public void deleteRestaurantById(int resId) {
		// TODO Auto-generated method stub
		Restaurant res=getHibernateTemplate().get(Restaurant.class, resId);
		getHibernateTemplate().delete(res);
	}

	public Restaurant getRestaurantById(int resId) {
		// TODO Auto-generated method stub
		Restaurant res=getHibernateTemplate().get(Restaurant.class, resId);
		return res;
	}


	public List<Restaurant> loadRestaurant() {
		// TODO Auto-generated method stub
		List resList=(List) getHibernateTemplate().find("from Restaurant");
		
		return resList;
	
	}

	public void updateRestaurant(Restaurant restaurant) {
		// TODO Auto-generated method stub
		getHibernateTemplate().update(restaurant);
	}

	public List<Restaurant> loadResBy(final int managerId) {
		// TODO Auto-generated method stub
		Manager manager=null;
	
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria cri = session.createCriteria(Restaurant.class);
				cri.add(Restrictions.eq("manager.managerId", new Integer(managerId)));
				List<Restaurant> result = cri.list();
				return result;
			}
		});
//		List list=getHibernateTemplate().find("from Manager where managerName=?", "test");
		return list;
	}

}
